<?php

/*
 *
 */

include('../../../sidebardbinfo-live.php');

// connect to the database
$tagcloudlink = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$tagcloudlink) {
	die('Problem connecting to database server : ' . mysql_error());
}

// make $dbname the current db
$db_selected = mysql_select_db($dbname, $tagcloudlink);
if (!$db_selected) {
	die ('Problem connecting to database : ' . mysql_error());
}

// get tag stats
$tagqtystmt = "SELECT k.word, COUNT(l.video_id) AS quantity
  FROM  tags_lookup l JOIN tags_keywords k ON (k.word_id=l.word_id)
  GROUP BY l.word_id
  ORDER BY k.word ASC";

$tagqtyres = mysql_query($tagqtystmt);

if (!$tagqtyres) {
	die ('Problem with query : ' . mysql_error());
}

// here we loop through the results and put them into a simple array:
// $tag['thing1'] = 12;
// $tag['thing2'] = 25;
// etc. so we can use all the nifty array functions
// to calculate the font-size of each tag
while ($tagqtyrow = mysql_fetch_array($tagqtyres)) {
    $tagsarray[stripslashes($tagqtyrow['word'])] = $tagqtyrow['quantity'];
}

// change these font sizes if you will
$max_size = 250; // max font size in %
$min_size = 75; // min font size in %

// get the largest and smallest array values
$max_qty = max(array_values($tagsarray));
$min_qty = min(array_values($tagsarray));

// find the range of values
$spread = $max_qty - $min_qty;
if (0 == $spread) { // we dont want to divide by zero
    $spread = 1;
}

// determine the font-size increment
// this is the increase per tag quantity (times used)
$step = ($max_size - $min_size)/($spread);

// loop through our tag array
foreach ($tagsarray as $key => $value) {

    // calculate CSS font-size
    // find the $value in excess of $min_qty
    // multiply by the font-size increment ($size)
    // and add the $min_size set above
    $size = $min_size + (($value - $min_qty) * $step);
    // uncomment if you want sizes in whole %:
    // $size = ceil($size);

    // you'll need to put the link destination in place of the #
    // (assuming your tag links to some sort of details page)
    echo "<a href=\"javascript:showTagVideos('".addslashes($key)."',1)\" style=\"font-size: ".$size."%\"";
    // perhaps adjust this title attribute for the things that are tagged
    echo ' title="'.$value.' things tagged with '.$key.'"';
    echo '>'.$key.'</a> ';
    // notice the space at the end of the link
}

mysql_close($tagcloudlink);
?>